<?php

namespace app\common\model;

use app\admin\model\usermanage\Users;
use app\admin\model\usermanage\UsersWage;
use app\api\model\GroupPv;
use app\api\model\UsersGroup;
use think\Cache;
use think\Log;
use think\Model;

/**
 *
 */
class TeamPv extends Model
{

    public static function newTeamPerformance($user_id,$type=0){
        $user = Users::get($user_id);

        if ($user->pv<=2900){
            return true;
        }

        //$beginThisweek = mktime(0, 0, 0, date('m'), date('d') - date('w') + 1, date('y'));
        //$endThisweek = mktime(23, 59, 59, date("m"), date("d") - date("w") + 7, date("Y"));

        $beginThisweek = strtotime(date('Y-m-d', strtotime('this week')));

        $endThisweek = strtotime(date('Y-m-d', strtotime('this week +6 days')))+86399;

        //$beginThisweek = 1761494400;
        //$endThisweek = 1762099199;
    
        Log::error('开始时间戳'.$beginThisweek);
        Log::error('结束时间戳'.$endThisweek);

        $A_group_list = UsersGroup::where(['type'=>0,'user_id'=>$user_id])
            ->order('createtime asc')->select();

        $A_group_pv = GroupPv::where(['first_group_id'=>$A_group_list[0]['id']])
            ->order('createtime asc')->select();
        Log::error('A组列表'.json_encode($A_group_pv));

        $list = UsersWage::where(['user_id' => $user_id])
            ->where('type','in',[2,3,4])
            ->order('createtime desc , id desc')
            ->select();

        Log::error('奖励的list的第一个type:'.$list[0]['type']);

        Log::error('奖励的list:'.json_encode($list));

        foreach ($A_group_pv as $k=>$v){
            $exist = UsersWage::where(['user_id' => $user_id,'type'=>4,'group_pv_id'=>$v['id']])
                ->where('createtime', 'between time', [$beginThisweek, $endThisweek])
                ->count();
            //->find();
            //var_dump($exist);die;
            Log::error('当前周的type=4是否存在:'.$exist);
            if ($exist>0) {
                return true;
            }

            Log::error('A组json'.json_encode($v));

            if (($v['first_pv'] >= 21060 && $v['new_pv'] >= 21060) || ($v['first_pv'] >= 28080 && $v['new_pv'] >= 14040) || ($v['first_pv'] >= 14040 && $v['new_pv'] >= 28080) && ($v['first_pv'] + $v['new_pv'] >= 42120)){

                if (count($list)==0){
                    $team_price = 2500;
                    $team_integral = 1000;
                    $count_1 = UsersWage::where(['user_id' => $user_id,'type'=>4])->count();
                    if ($count_1<2){
                        $team_integral += 2500;
                    }
                    $exist_1 = UsersWage::where(['user_id' => $user_id,'type'=>2,'group_pv_id'=>$v['id'],'status'=>0])
                        ->find();
                    if (!$exist_1){
                        Log::error('type0-222 团队业绩满足42120');
                        UsersWage::set($team_price,$user_id,2,0,$team_integral,$v['id']);
                    }
                } else {
                    //if ($list[0]['type']!=2){
                    if ($list[0]['type']==4){
                        $team_price = 2500;
                        $team_integral = 1000;
                        $count_1 = UsersWage::where(['user_id' => $user_id,'type'=>4])->count();
                        if ($count_1<2){
                            $team_integral += 2500;
                        }
                        $exist_1 = UsersWage::where(['user_id' => $user_id,'type'=>2,'group_pv_id'=>$v['id'],'status'=>0])
                            ->find();

                        if (!$exist_1){
                            Log::error('type1-222 团队业绩满足42120');
                            UsersWage::set($team_price,$user_id,2,0,$team_integral,$v['id']);
                        }
                    }
                }

            }
            if (($v['first_pv'] >= 63000 && $v['new_pv'] >= 63000) || ($v['first_pv'] >= 84000 && $v['new_pv'] >= 42000) || ($v['first_pv'] >= 42000 && $v['new_pv'] >= 84000) && ($v['first_pv'] + $v['new_pv'] >= 126000)) {

                if (count($list)==0){
                    $exist_2 = UsersWage::where(['user_id' => $user_id,'type'=>3,'group_pv_id'=>$v['id'],'status'=>0])
                        ->find();
                    if (!$exist_2){
                        $team_price = 5000;
                        $team_integral = 2000;
                        $count_2 = UsersWage::where(['user_id' => $user_id,'type'=>4])->count();
                        if ($count_2<2){
                            $team_price += 5000;
                        }
                        Log::error('type0-333 团队业绩满足126000');
                        UsersWage::set($team_price,$user_id,3,0,$team_integral,$v['id']);
                    }
                } else {

                    if (($list[0]['type']==2||$list[0]['type']==4)&&$list[0]['type']!=3){
                        $exist_2 = UsersWage::where(['user_id' => $user_id,'type'=>3,'group_pv_id'=>$v['id'],'status'=>0])
                            ->find();
                        if (!$exist_2){
                            $team_price = 5000;
                            $team_integral = 2000;
                            $count_2 = UsersWage::where(['user_id' => $user_id,'type'=>4])->count();
                            if ($count_2<2){
                                $team_price += 5000;
                            }
                            Log::error('type1-333 团队业绩满足126000');
                            UsersWage::set($team_price,$user_id,3,0,$team_integral,$v['id']);
                        }
                    }
                }
            }
            if (($v['first_pv'] >= 89550 && $v['new_pv'] >= 89550) || ($v['first_pv'] >= 119400 && $v['new_pv'] >= 59700) || ($v['first_pv'] >= 59700 && $v['new_pv'] >= 119400) && ($v['first_pv'] + $v['new_pv'] >= 179100)) {

                $exist_3 = UsersWage::where(['user_id' => $user_id,'type'=>4,'group_pv_id'=>$v['id'],'status'=>0])
                    ->find();
                if (!$exist_3){
                    $team_price = 7500;
                    $team_integral = 3000;
                    Log::error('type0-444 团队业绩满足179100');
                    UsersWage::set($team_price,$user_id,4,0,$team_integral,$v['id']);
                }
            }
        }

        $B_group_list = UsersGroup::where(['type'=>1,'user_id'=>$user_id])
            ->order('createtime asc')->select();
        $B_group_pv = GroupPv::where(['first_group_id'=>$B_group_list[0]['id']])
            ->order('createtime asc')->select();

        Log::error('B组列表'.json_encode($B_group_pv));
        foreach ($B_group_pv as $k=>$v){
            $exist = UsersWage::where(['user_id' => $user_id,'type'=>4,'group_pv_id'=>$v['id']])
                ->where('createtime', 'between time', [$beginThisweek, $endThisweek])
                ->count();

            if ($exist>0) {
                return true;
            }

            if (($v['first_pv'] >= 21060 && $v['new_pv'] >= 21060) || ($v['first_pv'] >= 28080 && $v['new_pv'] >= 14040) || ($v['first_pv'] >= 14040 && $v['new_pv'] >= 28080) && ($v['first_pv'] + $v['new_pv'] >= 42120)) {

                if (count($list)==0){
                    $team_price = 2500;
                    $team_integral = 1000;
                    $count_1 = UsersWage::where(['user_id' => $user_id,'type'=>4])->count();
                    if ($count_1<2){
                        $team_integral += 2500;
                    }
                    $exist_1 = UsersWage::where(['user_id' => $user_id,'type'=>2,'group_pv_id'=>$v['id'],'status'=>0])
                        ->find();
                    if (!$exist_1){
                        Log::error('type0-222 团队业绩满足42120');
                        UsersWage::set($team_price,$user_id,2,0,$team_integral,$v['id']);
                    }
                } else {
                    if ($list[0]['type']==4){
                        $team_price = 2500;
                        $team_integral = 1000;
                        $count_1 = UsersWage::where(['user_id' => $user_id,'type'=>4])->count();
                        if ($count_1<2){
                            $team_integral += 2500;
                        }
                        $exist_1 = UsersWage::where(['user_id' => $user_id,'type'=>2,'group_pv_id'=>$v['id'],'status'=>0])
                            ->find();
                        if (!$exist_1){
                            Log::error('type1-222 团队业绩满足42120');
                            UsersWage::set($team_price,$user_id,2,0,$team_integral,$v['id']);
                        }
                    }
                }

            }
            if (($v['first_pv'] >= 63000 && $v['new_pv'] >= 63000) || ($v['first_pv'] >= 84000 && $v['new_pv'] >= 42000) || ($v['first_pv'] >= 42000 && $v['new_pv'] >= 84000) && ($v['first_pv'] + $v['new_pv'] >= 126000)) {
                if (count($list)==0){
                    $exist_2 = UsersWage::where(['user_id' => $user_id,'type'=>3,'group_pv_id'=>$v['id'],'status'=>0])
                        ->find();
                    if (!$exist_2){
                        $team_price = 5000;
                        $team_integral = 2000;
                        $count_2 = UsersWage::where(['user_id' => $user_id,'type'=>4])->count();
                        if ($count_2<2){
                            $team_price += 5000;
                        }
                        Log::error('type0-333 团队业绩满足126000');
                        UsersWage::set($team_price,$user_id,3,0,$team_integral,$v['id']);
                    }
                } else {
                    if (($list[0]['type']==2||$list[0]['type']==4)&&$list[0]['type']!=3){
                        $exist_2 = UsersWage::where(['user_id' => $user_id,'type'=>3,'group_pv_id'=>$v['id'],'status'=>0])
                            ->find();
                        if (!$exist_2){
                            $team_price = 5000;
                            $team_integral = 2000;
                            $count_2 = UsersWage::where(['user_id' => $user_id,'type'=>4])->count();
                            if ($count_2<2){
                                $team_price += 5000;
                            }
                            Log::error('type1-333 团队业绩满足126000');
                            UsersWage::set($team_price,$user_id,3,0,$team_integral,$v['id']);
                        }
                    }
                }
            }
            if (($v['first_pv'] >= 89550 && $v['new_pv'] >= 89550) || ($v['first_pv'] >= 119400 && $v['new_pv'] >= 59700) || ($v['first_pv'] >= 59700 && $v['new_pv'] >= 119400) && ($v['first_pv'] + $v['new_pv'] >= 179100)) {
                $exist_3 = UsersWage::where(['user_id' => $user_id,'type'=>4,'group_pv_id'=>$v['id'],'status'=>0])
                    ->find();
                if (!$exist_3){
                    $team_price = 7500;
                    $team_integral = 3000;
                    Log::error('type0-444 团队业绩满足179100');
                    UsersWage::set($team_price,$user_id,4,0,$team_integral,$v['id']);
                }
            }
        }

        Log::error('--------------------');
        return true;
    }
    
}